package com.fa.wisdom.ui.fragment;

import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseFragment;
import com.fa.wisdom.utils.ScreenUtil;
import com.just.agentweb.AgentWeb;

public class ShopNFragment extends BaseFragment {
    View statusBarPlace;
    WebView webView;
    ProgressBar mProgressBar;
    private String url;
    LinearLayout ll_root;
    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_shop_n, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ll_root=view.findViewById(R.id.ll_root);
        statusBarPlace = view.findViewById(R.id.view_status_bar_place);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        mProgressBar=view.findViewById(R.id.myProgressBar);
        webView=view.findViewById(R.id.webView);
        AgentWeb  mAgentWeb = AgentWeb.with(requireActivity()).setAgentWebParent(ll_root, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator().createAgentWeb().ready().go("https://cr.feiaikeji.cn/");
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            setWebView();
        }
    }
    private void setWebView() {
        url ="https://cr.feiaikeji.cn/";
//        url="https://www.baidu.com/";
        WebSettings settings = webView.getSettings();
        //WebView加载web资源
        int screenDensity = getResources().getDisplayMetrics().densityDpi;
        WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.MEDIUM;
        switch (screenDensity) {
            case DisplayMetrics.DENSITY_LOW:
                zoomDensity = WebSettings.ZoomDensity.CLOSE;
                break;
            case DisplayMetrics.DENSITY_MEDIUM:
                zoomDensity = WebSettings.ZoomDensity.MEDIUM;
                break;
            case DisplayMetrics.DENSITY_HIGH:
                zoomDensity = WebSettings.ZoomDensity.FAR;
                break;
            default:
                break;
        }
        settings.setDefaultZoom(zoomDensity);

        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 不使用缓存
//        webView.addJavascriptInterface(new JsInterface(com.lyhengtongwl.zqsnews.ui.activity.WebActivity.this), "android");
        String ua = webView.getSettings().getUserAgentString();
        settings.setUserAgentString(ua + "android/ts");
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//把html中的内容放大webview等宽的一列中
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDisplayZoomControls(true);//隐藏Zoom缩放按钮


        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        //解除数据阻止
        settings.setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
            settings.setMixedContentMode(2);
        }
//        Map<String, String> map = new HashMap<>();
//        my_jessionId = SPUtils.get(App.getContext(), Constant.CommonInfo.JSESSIONID, "") + "";
//        map.put("jsessionid", my_jessionId);

//        webView.loadUrl(url, map);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    if (mProgressBar.getVisibility() == View.GONE)
                        mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //网页在webView中打开
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {//安卓5.0的加载方法
                    view.loadUrl(request.toString());
                } else {//5.0以上的加载方法
                    view.loadUrl(request.getUrl().toString());
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    webView.getSettings()
                            .setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                }
                handler.proceed();//接受证书
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        webView.loadUrl(url);
    }

}
